<?php
/**
 * @author GhostWei
 * @date Aug 6, 2012
 */
class controller_admin_system_user extends system_user_controller_panel {
	
	private $concept = null;
	public function init() {
		if(!parent::init()) {
			return ;
		}
		$this->concept = new system_user_concept();
		$this->_crumbs()->addPage(system_nav_page::create($this->language('System'))->setRoute('system/index'));
		$this->_crumbs()->addPage(system_nav_page::create($this->language('User Account'))->setRoute('system/user/index'));
	}
	
	public function action_addnew() {
		$row = $this->concept->create();
		$row->setTimezone(system_nav_language::getInstance()->getDefaultTimezone());
		$row = $this->_saveRow($row, array('operation' => 'create'));
		$this->view->row = $row;
		$this->view->referer = $this->referer;
		$this->_crumbs()->addPage(system_nav_page::create($this->language('New Account'))->setRoute('system/user/addnew'));
		$this->render('system/user/row.phtml');
	}
	
	public function action_edit() {
		$id = intval($this->_param('id'));
		if(!($row = $this->concept->fetch(array('user.id = ?' => $id)))) {
			throw new system_exception('User Account is not existing');
		}
		$row = $this->_saveRow($row, array('operation' => 'edit'));
		$this->view->row = $row;
		$this->view->referer = $this->referer;
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Edit Account'))->setRoute('system/user/edit')->setParams(array('id' => $row->getId())));
		$this->render('system/user/row.phtml');
	}
	
	public function _saveRow($row, $options = array()) {
		$permissionList = array();
		$concept = new system_user_permission_concept();
		foreach($concept->fetchAll(array('user_id = ?' => intval($row->getId()))) AS $_row) {
			$permissionList[] = $_row->getGroupId();
		}
		$this->view->permissionList = $permissionList;
		if(!$this->getRequest()->isPost()) {
			return $row;
		}
		$this->view->permissionList = $permissionList = (array) $this->_post('permission');
		$this->referer = $this->_post('referer');
		$row->setIdentity(trim($this->_post('identity')));
		$row->setEmail(trim($this->_post('email')));
		$row->setNickname(trim($this->_post('nickname')));	
		$row->setTimezone(trim($this->_post('timezone')));
		foreach($permissionList AS $gid) {
			$row->addPermissionList($gid);
		}	
		$this->_saveToDatabase($row, 'logo', 'image', null, $options);		
		return $row;
	}
		
	public function action_index() {
		$this->_goRoute('system/user/list');
		$this->render();
	}
	
	public function action_list() {
		$this->_batchDelete($this->concept, 'id', 'user.id');		
		$adapter = new system_paginator_adapter_concept($this->concept);
		$adapter->setCurrentPageNumber(intval($this->_param('page', 1)));

		$_order = array("id ASC");
		if(1 && $order = strtolower($this->_get('order'))) {
			$sort = intval($this->_get('sort'));
			switch($order) {
				case 'time': {
					$order = 'lastupdate';
					break;
				}
				case 'identity': case 'email': {
					break;
				}
				default: {
					$order = 'user.id';
					break;
				}
			}
			$_sort = !$sort ? 'DESC' : 'ASC';
			$_order = array("$order $_sort");
		}
		$adapter->setLimit(30);
		$adapter->order($_order);
		$this->view->list = new system_paginator($adapter);
		$this->_crumbs()->addPage(system_nav_page::create($this->language('List'))->setRoute('system/user/list'));
		$this->render();
	}
	
}
